# Contributor: Leo <thinkabit.ukim@gmail.com>
# Contributor: stef <l0ls0fo2i@ctrlc.hu>
# Contributor: André Klitzing <aklitzing@gmail.com>
# Maintainer: psykose <alice@ayaya.dev>
pkgname=zstd
pkgver=1.5.2
pkgrel=3
pkgdesc="Zstandard - Fast real-time compression algorithm"
url="https://www.zstd.net/"
arch="all"
license="BSD-3-Clause GPL-2.0-or-later"
depends_dev="$pkgname-static"
makedepends="cmake grep samurai"
checkdepends="file"
subpackages="
	$pkgname-static
	$pkgname-libs
	$pkgname-dev
	$pkgname-doc
	$pkgname-frugal
	"
source="zstd-$pkgver.tar.gz::https://github.com/facebook/zstd/archive/v$pkgver.tar.gz"

# secfixes:
#   1.4.9-r0:
#     - CVE-2021-24032
#   1.4.1-r0:
#     - CVE-2021-24031
#   1.3.8-r0:
#     - CVE-2019-11922

export CFLAGS="${CFLAGS/-Os/-O2}"
export CXXFLAGS="${CXXFLAGS/-Os/-O2}"

build() {
	if [ "$CBUILD" != "$CHOST" ]; then
		CMAKE_CROSSOPTS="-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_HOST_SYSTEM_NAME=Linux"
	fi
	cmake -B build-zstd -G Ninja -S build/cmake \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_BUILD_TYPE=None \
		-DBUILD_TESTING="$(want_check && echo ON || echo OFF)" \
		-DZSTD_PROGRAMS_LINK_SHARED=ON \
		$CMAKE_CROSSOPTS
	cmake --build build-zstd
}

check() {
	ctest --test-dir build-zstd --output-on-failure
}

package() {
	DESTDIR="$pkgdir" cmake --install build-zstd
	install -Dm755 build-zstd/programs/zstd-frugal -t "$pkgdir"/usr/bin
}

frugal() {
	pkgdesc="$pkgdesc (compressor only)"
	amove usr/bin/zstd-frugal
}

sha512sums="
e107508a41fca50845cc2494e64adaba93efb95a2fa486fc962510a8ba4b2180d93067cae9870f119e88e5e8b28a046bc2240b0b23cdd8933d1fb1a6a9668c1e  zstd-1.5.2.tar.gz
"
